import axios from 'axios'
let actions = {
    incrementasync(){},
    decrementasync(){},
    async addcart(context,item){
        // 增加购物车
        
        let {data} = await axios.get("http://localhost:3000/carts")
        let a = data.find((it)=>{
            return it.imgsrc===item.imgsrc
        })
        console.log(a)
        delete item.id
        // 添加
        await axios.post("http://localhost:3000/carts",item)
        
        context.dispatch("getCarts")
    },
    async deleteCart(context,id){
        // 删除购物车
        console.log(context)
        await axios.delete("http://localhost:3000/carts/"+id)
        context.dispatch("getCarts")
    },
    async getProducts(context){
        // 获取商品
        // actions 不能写修改state状态的代码
        // axios.get('http://localhost:3000/products')
        // .then((res)=>{
        //     context.commit("",res.data)
        // })
        let {data} = await axios.get('http://localhost:3000/products')
        context.commit("productsget",data)
    },
    async getCarts(context){
        // 获取购物车
        console.log("====")
        let {data} = await axios.get("http://localhost:3000/carts")
        context.commit("cartsget",data)
    }
}
export default actions